package com.happyscan138.makefun;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.happyscan138.makefun.Fragment.Saoku_Face_Fragment;
import com.happyscan138.makefun.Fragment.Saoku_Picture_Fragment;
import com.happyscan138.makefun.Fragment.Saoku_Text_Fragment;
import com.happyscan138.makefun.Fragment.Saoku_Video_Fragment;
import com.happyscan138.makefun.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/20.
 */

public class Saoku extends AppCompatActivity   implements Saoku_Text_Fragment.CallBackValue {
    private ViewPager mPager; //定义viewPager
    private ArrayList<Fragment> list;//定义要装fragment的列表
    private  Fragment Text_Fragment;
    private  Fragment Picture_Fragment;
    private  Fragment Video_Fragment;
    private  Fragment Face_Fragment;
    private RadioButton buttonText, buttonPicture,buttonVideo,buttonFace;
    private Resources resources;
    private int pWidth;
    private int offset ;// 图片偏移量
    private ImageView ivBottomLine;// 横向下划线
    private final static int num = 6 ;
    private int position_one;
    private int currentIndex = 0;// 当前标签位置
    private int section;
    private ImageButton back;
    private String SaokuText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saoku);
        resources = getResources();
        InitWidth();
        InitTextView();
        InitViewPager();


    }
    private void InitWidth(){
        ivBottomLine = (ImageView) findViewById(R.id.saoku_line);
        BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.mipmap.up_tab);
        Bitmap bmp = bd.getBitmap();
        pWidth = ivBottomLine.getLayoutParams().width;
        pWidth = bmp.getWidth();
        // 获取手机屏幕分辨率(宽度)
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;

        // 每一个标签页所占：offset+pWidth+offset...[以此类推]
        offset = (screenW /num -pWidth)/2 ;//移动偏移量
        int avg = (int) (screenW / num);
        section = offset*2+pWidth ;
        int position_one=offset+avg;
        // 通过LayoutParams.setMargins属性设置偏移量
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);  // , 1是可选写的
        lp.setMargins(position_one, 0, 0, 0);
        ivBottomLine.setLayoutParams(lp);


    }

    private void InitTextView() {
        buttonText = (RadioButton) findViewById(R.id.saoku_newbest);
        buttonPicture = (RadioButton) findViewById(R.id.saoku_picture);
        buttonVideo = (RadioButton) findViewById(R.id.saoku_video);
        buttonFace = (RadioButton) findViewById(R.id.saoku_face);

        back=(ImageButton)findViewById(R.id.saoku_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        buttonText.setOnClickListener(new MyOnClickListener(0));
        buttonPicture.setOnClickListener(new MyOnClickListener(1));
        buttonVideo.setOnClickListener(new MyOnClickListener(2));
        buttonFace.setOnClickListener(new MyOnClickListener(3));
    }
    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.saoku_viewpager);
        list = new ArrayList<Fragment>();

        Text_Fragment = new Saoku_Text_Fragment();
        Picture_Fragment = new Saoku_Picture_Fragment();
        Video_Fragment= new Saoku_Video_Fragment();
        Face_Fragment =new Saoku_Face_Fragment();

        list.add(Text_Fragment);
        list.add(Picture_Fragment);
        list.add(Video_Fragment);
        list.add(Face_Fragment);

        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),list));
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Animation animation = null;
                switch (position) {
                    case 0:
                        if (position== 0) {
                            animation = new TranslateAnimation(section * currentIndex , section * position, 0, 0);
                        }
                        break;
                    case 1:
                        if (position == 1) {
                            animation = new TranslateAnimation(section * currentIndex, section * position, 0, 0);

                        }
                        break;
                    case 2:
                        if (position == 2) {
                            animation = new TranslateAnimation(section * currentIndex, section * position, 0, 0);
                        }
                        break;
                    case 3:
                        if(position==3){
                            animation=new TranslateAnimation(section * currentIndex, section * position, 0, 0);
                        }
                        break;

                }
                currentIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(300);
                ivBottomLine.startAnimation(animation);
            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void SendMessageValue(String strValue) {
        SaokuText=strValue;
        log.e("strValue",SaokuText);

    }


    private class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };
}
